<template>
  <div class="main">
    <div class="header">
      <el-input placeholder="请输入员工姓名" clearable v-model="name">
        <i
          slot="prefix"
          class="el-input__icon el-icon-search"
          @click="handleSearch"
        ></i>
      </el-input>
      <el-button
        type="primary"
        icon="el-icon-plus"
        @click="$router.push({ name: 'AddEmployee', query: { isEdit: false } })"
        >添加员工</el-button
      >
    </div>
    <div class="  ">
      <el-table
        stripe
        border
        :data="tableData"
        style="width: 100%; margin-top: 20px"
      >
        <el-table-column prop="name" label="员工姓名"> </el-table-column>
        <el-table-column prop="username" label="账号"> </el-table-column>
        <el-table-column prop="phone" label="手机号"> </el-table-column>
        <el-table-column prop="status" label="账号状态">
          <template v-slot="{ row }">
            {{ row.status === 1 ? "正常" : "已禁用" }}
          </template>
        </el-table-column>
        <el-table-column label="操作">
          <template v-slot="{ row }">
            <el-link type="primary">编辑</el-link>
            <el-divider direction="vertical"></el-divider>
            <el-popconfirm
              @onConfirm="handleChangeStatus(row)"
              title="确认调整该账号的状态?"
            >
              <el-link slot="reference" type="danger">{{
                row.status === 1 ? "禁用" : "启用"
              }}</el-link>
            </el-popconfirm>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <div class="footer">
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="pageInfo.page"
        :page-sizes="[10, 20, 30, 50]"
        :page-size="pageInfo.pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      >
      </el-pagination>
    </div>
  </div>
</template>

<script>
import { getEmployees, changeEmployeeStatus } from "@/api/employee";
export default {
  data() {
    return {
      name: "",
      tableData: [],
      pageInfo: {
        page: 1,
        pageSize: 10,
      },
      total: 0,
    };
  },
  created() {
    this.init();
  },
  mounted() {
    this.getData();
  },
  methods: {
    init() {
      let params;
      if (this.name) {
        params = { ...this.pageInfo, name: this.name };
      } else {
        params = { ...this.pageInfo };
      }
      getEmployee(params).then((res) => {
        this.tableData = res.data.records;
        this.total = res.data.total;
      });
    },
    getData() {
      getEmployees({ ...this.pageInfo, name: this.name }).then((res) => {
        this.tableData = res.data.records;
        this.total = res.data.total;
      });
    },
    handleSizeChange(val) {
      this.pageInfo.page = 1;
      this.pageInfo.pageSize = val;
      this.getData();
    },
    handleCurrentChange(val) {
      this.pageInfo.page = val;
      this.getData();
    },
    handleSearch() {
      this.getData();
    },
    handleChangeStatus(row) {
      const data = {
        id: row.id,
        status: +!row.status,
      };
      changeEmployeeStatus(data).then((res) => {
        this.$message.success("状态改变成功");
        this.getData();
      });
    },
  },
};
</script>

<style lang="scss" scoped>
.main {
  padding: 30px 28px;
  background: #fff;
  .header {
    display: flex;
    justify-content: space-between;
    .el-input {
      width: 250px;
    }
  }
  .footer {
    margin-top: 20px;
    display: flex;
    justify-content: center;
  }
}
</style>
